package com.qq.spirit;

import java.io.UnsupportedEncodingException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

public class CryptUtil
{

	static sun.misc.BASE64Encoder encoder = new sun.misc.BASE64Encoder();
	static sun.misc.BASE64Decoder decoder = new sun.misc.BASE64Decoder();

	private CryptUtil()
	{

	}

	public static String encode(String bstr)
	{
		try
		{
			return encoder.encode(bstr.getBytes("utf-8"));
		} catch (UnsupportedEncodingException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "";
	}

	/**
	 * 解码
	 * 
	 * @param str
	 * @return string
	 */
	public static String decode(String str)
	{
		try
		{
			byte[] bt = decoder.decodeBuffer(str);
			return new String(bt, "utf-8");
		} catch (Exception e)
		{
			e.printStackTrace();
		}
		return "";
	}

	public static void main(String[] args)
	{
		String content = "123123";
		String s = CryptUtil.encode(content);
		System.out.println(s);
		System.out.println(CryptUtil.decode(s));
	}
}